package com.ee.action;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.ee.bean.Equipment;
import com.ee.service.EquipmentService;
import com.opensymphony.xwork2.ActionSupport;

@Component("equipmentAction")
@Scope("prototype")
public class EquipmentAction extends ActionSupport  implements ServletRequestAware, ServletResponseAware{
	private File img;
	private String imgFileName;
	private String imgContentType;
	private Equipment equipment;
	private EquipmentService equipmentService;
	private HttpServletRequest request;
	private HttpServletResponse response;
	/**
	 * 删除设备
	 * @return
	 */
	public String delete(){
		equipmentService.delete(equipment);
		return "_success";
	}
	/**
	 * 添加设备
	 * @return
	 */
	public String add(){
		try {
			if(this.getImg() != null){
				String froot = ServletActionContext.getServletContext().getRealPath("imgs");
				File simg = new File(froot, imgFileName);
				FileUtils.copyFile(img, simg);
				this.equipment.setImg(imgFileName);
			}else{
				if(this.equipment.getId() > 0){
					Equipment temp = equipmentService.getEquipmentById(this.equipment.getId());
					this.equipment.setImg(temp.getImg());
				}
			}
			if("".equals(this.equipment.getId()) || this.equipment.getId() == 0){
				equipment.setStatus("未安装");
				equipmentService.add(equipment);
			}else{
				equipmentService.update(equipment);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			request.setAttribute("equipments", equipmentService.getAllEquipments());
			request.setAttribute("status", "error");
			request.setAttribute("message", "文件过大，请选择小容量文件");
			return "img_input";
		}
		return "_success";
	}
	/**
	 * 设备列表
	 * @return
	 */
	public String list(){
		request.setAttribute("equipments",equipmentService.getAllEquipments());
		return "list";
	}
	
	public Equipment getEquipment() {
		return equipment;
	}
	public void setEquipment(Equipment equipment) {
		this.equipment = equipment;
	}
	public EquipmentService getEquipmentService() {
		return equipmentService;
	}
	
	@Resource
	public void setEquipmentService(EquipmentService equipmentService) {
		this.equipmentService = equipmentService;
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;		
	}

	public File getImg() {
		return img;
	}

	public void setImg(File img) {
		this.img = img;
	}

	public String getImgFileName() {
		return imgFileName;
	}

	public void setImgFileName(String imgFileName) {
		this.imgFileName = imgFileName;
	}

	public String getImgContentType() {
		return imgContentType;
	}

	public void setImgContentType(String imgContentType) {
		this.imgContentType = imgContentType;
	}
	
}
